import 'package:flutter/material.dart';
import 'package:lazy_food/components/searchBar/search_bar.dart';
import 'package:lazy_food/home/follow/follow.dart';
import 'package:lazy_food/home/other/other.dart';
import 'package:lazy_food/home/rank/rank.dart';
import 'package:lazy_food/home/recommend/recommend.dart';
import 'package:lazy_food/home/vip/vip.dart';
import 'package:lazy_food/home/weekly/weekly.dart';
import 'package:lazy_food/models/tab_model.dart';
import 'package:lazy_food/net/network.dart';
import 'package:lazy_food/search/search.dart';

class HomePage extends StatefulWidget {
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  int _counter = 0;
  TextEditingController _textEditingController = TextEditingController();
  List<TabModel> _tabs = [];
  late TabController _tabController;

  void initState() {
    super.initState();

    Network.getHomeTabs().then((value) {
      setState(() {
        _tabs = value;
        _tabController =
            TabController(length: _tabs.length, vsync: this, initialIndex: 1);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        title: _searchBar(),
        shadowColor: null,
        backgroundColor: Colors.black,
        bottom: _tabBar(),
      ),
      body: _body(),
    );
  }

  Widget _searchBar() {
    return SearchBar(
      height: 34,
      hit: '搜索食材、菜谱',
      hitStyle: TextStyle(color: Colors.white, fontSize: 14),
      isEditing: false,
      prefixIcon: Icon(
        Icons.search,
        color: Colors.white,
        size: 20,
      ),
      onTap: () {
        Navigator.push(
          context,
          PageRouteBuilder(
            pageBuilder: (context, animation, secondary) {
              return SearchPage();
            },
            transitionsBuilder: (context, animation, secondary, child) {
              const begin = Offset(0.0, 0.0);
              const end = Offset.zero;
              final tween = Tween(begin: begin, end: end);
              final offsetAnimation = animation.drive(tween);
              return SlideTransition(
                position: offsetAnimation,
                child: child,
              );
            },
          ),
        );
      },
    );
  }

  PreferredSizeWidget? _tabBar() {
    return _tabs.length > 0
        ? TabBar(
            tabs: _tabs.map((e) => Text(e.text)).toList(),
            controller: _tabController,
            isScrollable: true,
            padding: EdgeInsets.all(10),
            unselectedLabelColor: Colors.white54,
            labelColor: Colors.black,
            labelStyle: TextStyle(fontSize: 16),
            indicatorPadding: EdgeInsets.fromLTRB(0, -5, 0, -5),
            indicator: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(16)),
            ),
          )
        : null;
  }

  Widget? _body() {
    return _tabs.length > 0
        ? TabBarView(
            controller: _tabController,
            children: _tabs.map((e) {
              if (e.ident == 'rcmd') {
                return RecommendPage(tab: e);
              } else if (e.ident == 'follow') {
                return FollowPage(tab: e);
              } else if (e.ident == 'vip') {
                return VipPage(tab: e);
              } else if (e.ident == 'rank') {
                return RankPage(tab: e);
              } else if (e.ident == 'weekly') {
                return WeeklyPage(tab: e);
              } else {
                return OtherPage(tab: e);
              }
            }).toList(),
          )
        : null;
  }
}
